package com.shelpe.services.sustainer.entity.jpa;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;

import com.shelpe.services.sustainer.entity.jpa.AutoProductQueue.QueuePK;
import com.shelpe.services.sustainer.utils.EntityUtils.Day2IntConverter;

import lombok.Data;

@Entity(name="auto_pro_product_queue")
@IdClass(QueuePK.class)
@Data
public class AutoProductQueue {

	@Id
	@Column(name="product_id")
	private long productID;
	@Id
	@Column(name="campaign_id")
	private long campaignID;
	
	@Column(name="user_id")
	private long userID;
	@Column(name="adgroup_id")
	private long adgroupID;
	private int progress;
	@Column(name="msg")
	private String message = "";
	
	private int flag;
	
	@Convert(converter = StatusConverter.class)
	private Status status;
	
	@Column(name="addtime")
	@Convert(converter=Day2IntConverter.class)
	private Date createTime;
	@Column(name="updated")
	@Convert(converter=Day2IntConverter.class)
	private Date lastModifiedTime;
	
	@Data
	public static class QueuePK implements Serializable{
		private static final long serialVersionUID = 1L;
		@Column(name="product_id")
		private long productID;
		@Column(name="campaign_id")
		private long campaignID;
	}
	
	public static enum Status{
		added(0), updating(1), success(2), failed(-1);
		
		private int value;
		
		private Status(int v){
			this.value = v;
		}
		
		public int getValue(){
			return this.value;
		}
		
	}
	
	public static class StatusConverter implements AttributeConverter<Status, Integer>{

		@Override
		public Integer convertToDatabaseColumn(Status attribute) {
			return attribute!=null?attribute.value:Status.failed.getValue();
		}

		@Override
		public Status convertToEntityAttribute(Integer dbData) {
			Status status = Status.failed;
			Status.values();
			for(Status s: Status.values()){
				if(s.getValue() == dbData){
					status = s;
					break;
				}
			}
			return status;
		}
	}
}
